package com.wande.dataplatform.filecollection.common.enums;

/**
 * 文件采集模块错误码枚举
 *
 * @author wande
 */
public enum FileCollectionErrorCode {

    // 采集规则相关错误 (10001-10099)
    RULE_NOT_FOUND(10001, "采集规则不存在"),
    RULE_CODE_DUPLICATE(10002, "采集规则编码已存在"),
    RULE_ALREADY_ENABLED(10003, "采集规则已启用"),
    RULE_ALREADY_DISABLED(10004, "采集规则已禁用"),
    RULE_CONFIG_INVALID(10005, "采集规则配置无效"),
    RULE_HAS_RUNNING_TASK(10006, "采集规则有正在运行的任务，无法删除"),

    // 映射规则相关错误 (10101-10199)
    MAPPING_RULE_NOT_FOUND(10101, "映射规则不存在"),
    MAPPING_RULE_CODE_DUPLICATE(10102, "映射规则编码已存在"),
    MAPPING_CONFIG_INVALID(10103, "映射配置无效"),
    FIELD_MAPPING_INVALID(10104, "字段映射配置无效"),
    FIELD_TYPE_MISMATCH(10105, "字段类型不匹配"),
    TARGET_TABLE_NOT_FOUND(10106, "目标表不存在"),
    TARGET_FIELD_NOT_FOUND(10107, "目标字段不存在"),

    // 文件相关错误 (10201-10299)
    FILE_NOT_FOUND(10201, "文件不存在"),
    FILE_TYPE_NOT_SUPPORTED(10202, "不支持的文件类型"),
    FILE_TYPE_NOT_ALLOWED(10202, "文件类型不允许"),
    FILE_SIZE_EXCEEDED(10203, "文件大小超过限制"),
    FILE_UPLOAD_FAILED(10204, "文件上传失败"),
    FILE_UPLOAD_ERROR(10204, "文件上传失败"),
    FILE_DOWNLOAD_FAILED(10205, "文件下载失败"),
    FILE_DELETE_FAILED(10206, "文件删除失败"),
    FILE_MD5_MISMATCH(10207, "文件MD5校验失败"),
    FILE_MD5_ERROR(10207, "文件MD5校验失败"),
    FILE_VIRUS_DETECTED(10208, "文件包含病毒"),
    FILE_COLLECTION_FAILED(10209, "文件采集失败"),
    FILE_EMPTY(10210, "文件为空"),
    FILE_MD5_FAILED(10211, "计算文件MD5失败"),
    FILE_FETCH_ERROR(10212, "文件获取失败"),
    FILE_SOURCE_NOT_FOUND(10213, "文件源不存在"),
    FILE_PROCESS_ERROR(10214, "文件处理失败"),

    // 文件解析相关错误 (10301-10399)
    PARSE_ERROR(10301, "文件解析失败"),
    FILE_PARSE_ERROR(10301, "文件解析失败"),
    PARSER_NOT_FOUND(10302, "未找到对应的解析器"),
    UNSUPPORTED_FILE_TYPE(10302, "不支持的文件类型"),
    PARSE_CONFIG_INVALID(10303, "解析配置无效"),
    FILE_FORMAT_ERROR(10304, "文件格式错误"),
    FILE_ENCODING_ERROR(10305, "文件编码错误"),
    FIELD_DETECT_FAILED(10306, "字段检测失败"),
    XPATH_EXPRESSION_INVALID(10307, "XPath表达式无效"),
    JSONPATH_EXPRESSION_INVALID(10308, "JSONPath表达式无效"),

    // 数据校验相关错误 (10401-10499)
    VALIDATION_FAILED(10401, "数据校验失败"),
    FIELD_NOT_NULL_VIOLATION(10402, "字段不能为空"),
    FIELD_FORMAT_INVALID(10403, "字段格式不正确"),
    FIELD_RANGE_VIOLATION(10404, "字段值超出范围"),
    FIELD_ENUM_VIOLATION(10405, "字段值不在枚举范围内"),
    FIELD_UNIQUE_VIOLATION(10406, "字段值重复"),
    FIELD_LENGTH_EXCEEDED(10407, "字段长度超过限制"),

    // 数据加载相关错误 (10501-10599)
    LOAD_ERROR(10501, "数据加载失败"),
    DATABASE_CONNECTION_FAILED(10502, "数据库连接失败"),
    SQL_EXECUTION_FAILED(10503, "SQL执行失败"),
    TRANSACTION_ROLLBACK(10504, "事务回滚"),
    BATCH_INSERT_FAILED(10505, "批量插入失败"),
    DORIS_STREAM_LOAD_FAILED(10506, "Doris Stream Load失败"),
    DATA_TRANSFORM_ERROR(10507, "数据转换失败"),

    // 任务相关错误 (10601-10699)
    TASK_NOT_FOUND(10601, "采集任务不存在"),
    TASK_ALREADY_RUNNING(10602, "任务正在运行中"),
    TASK_NOT_RUNNING(10603, "任务未运行"),
    TASK_STOP_FAILED(10604, "任务停止失败"),
    TASK_RETRY_FAILED(10605, "任务重试失败"),
    TASK_TIMEOUT(10606, "任务执行超时"),

    // 文件源相关错误 (10701-10799)
    SOURCE_CONNECTION_FAILED(10701, "文件源连接失败"),
    SOURCE_AUTH_FAILED(10702, "文件源认证失败"),
    SOURCE_PATH_NOT_FOUND(10703, "文件源路径不存在"),
    SOURCE_PERMISSION_DENIED(10704, "文件源权限不足"),
    FTP_CONNECTION_FAILED(10705, "FTP连接失败"),
    SFTP_CONNECTION_FAILED(10706, "SFTP连接失败"),
    HTTP_REQUEST_FAILED(10707, "HTTP请求失败"),
    OSS_CONNECTION_FAILED(10708, "对象存储连接失败"),
    UNSUPPORTED_SOURCE_TYPE(10709, "不支持的文件源类型"),

    // 调度相关错误 (10801-10899)
    SCHEDULE_CREATE_FAILED(10801, "调度任务创建失败"),
    SCHEDULE_CREATE_ERROR(10801, "调度任务创建失败"),
    SCHEDULE_UPDATE_FAILED(10802, "调度任务更新失败"),
    SCHEDULE_UPDATE_ERROR(10802, "调度任务更新失败"),
    SCHEDULE_DELETE_FAILED(10803, "调度任务删除失败"),
    SCHEDULE_DELETE_ERROR(10803, "调度任务删除失败"),
    SCHEDULE_TRIGGER_FAILED(10804, "调度任务触发失败"),
    TASK_TRIGGER_ERROR(10804, "任务触发失败"),
    TASK_PAUSE_ERROR(10805, "任务暂停失败"),
    TASK_RESUME_ERROR(10806, "任务恢复失败"),
    RULE_DISABLED(10807, "采集规则已禁用"),
    DOLPHINSCHEDULER_API_ERROR(10808, "DolphinScheduler API调用失败"),
    CRON_EXPRESSION_INVALID(10809, "Cron表达式无效"),

    // MinIO相关错误 (10901-10999)
    MINIO_CONNECTION_FAILED(10901, "MinIO连接失败"),
    MINIO_BUCKET_NOT_FOUND(10902, "MinIO存储桶不存在"),
    MINIO_UPLOAD_FAILED(10903, "MinIO上传失败"),
    MINIO_DOWNLOAD_FAILED(10904, "MinIO下载失败"),
    MINIO_DELETE_FAILED(10905, "MinIO删除失败"),

    // 系统相关错误 (11001-11099)
    INVALID_PARAMETER(11000, "无效参数"),
    SYSTEM_ERROR(11001, "系统错误"),
    NETWORK_ERROR(11002, "网络错误"),
    TIMEOUT_ERROR(11003, "操作超时"),
    RESOURCE_EXHAUSTED(11004, "资源耗尽"),
    THREAD_POOL_FULL(11005, "线程池已满"),
    MEMORY_OVERFLOW(11006, "内存溢出"),

    // 历史记录相关错误 (11101-11199)
    FILE_RECORD_NOT_FOUND(11101, "文件记录不存在"),
    FILE_PREVIEW_FAILED(11102, "文件预览失败"),
    ERROR_RECORD_NOT_FOUND(11103, "错误记录不存在");

    private final int code;
    private final String message;

    FileCollectionErrorCode(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    /**
     * 根据错误码获取枚举
     */
    public static FileCollectionErrorCode fromCode(int code) {
        for (FileCollectionErrorCode errorCode : values()) {
            if (errorCode.getCode() == code) {
                return errorCode;
            }
        }
        return SYSTEM_ERROR;
    }
}
